package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.context.LoginContext;
import cn.itsource.ymcc.dto.KillPlaceOrderDto;
import cn.itsource.ymcc.dto.PlaceOrderDto;
import cn.itsource.ymcc.service.ICourseOrderService;
import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.query.CourseOrderQuery;
import cn.itsource.ymcc.result.JSONResult;
import cn.itsource.ymcc.result.PageList;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/courseOrder")
public class CourseOrderController {

    @Autowired
    public ICourseOrderService courseOrderService;

    /**
     * 防止表达重复提交的令牌
     */
    @RequestMapping(value="/createTokenForOrder",method= RequestMethod.GET)
    public JSONResult createTokenForOrder(){
        return JSONResult.success(courseOrderService.createTokenForOrder());
    }

    /**
     * 普通下单
     */
    @RequestMapping(value="/placeOrder",method= RequestMethod.POST)
    public JSONResult placeOrder(@RequestBody PlaceOrderDto dto){
        return courseOrderService.placeOrder(dto);
    }


    /**
     * 秒杀下单
     */
    @RequestMapping(value="/killPlaceOrder",method= RequestMethod.POST)
    public JSONResult killPlaceOrder(@RequestBody KillPlaceOrderDto dto){
        return courseOrderService.killPlaceOrder(dto);
    }


    /**
     * 根据单号查询订单
     */
    @RequestMapping(value="/one/{orderNo}",method= RequestMethod.GET)
    public JSONResult selectByOrderNo(@PathVariable("orderNo")String orderNo){
        return JSONResult.success(JSON.toJSONString(courseOrderService.selectByOrderNo(orderNo)));
    }


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody CourseOrder courseOrder){
        if(courseOrder.getId()!=null){
            courseOrderService.updateById(courseOrder);
        }else{
            courseOrderService.insert(courseOrder);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseOrderService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseOrderService.selectById(id));
    }


    /**
    * 查看所有
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseOrderService.selectList(null));
    }


    /**
    * 分页查询数据
    */
    @RequestMapping(value = "/forUser",method = RequestMethod.POST)
    public JSONResult json(@RequestBody CourseOrderQuery query){
        query.setUserId(LoginContext.getLogin().getId());
        PageList page = courseOrderService.selectPage(query);
        return JSONResult.success(page);
    }

    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult pagelist(@RequestBody CourseOrderQuery query){
        PageList page = courseOrderService.selectPage(query);
        return JSONResult.success(page);
    }
}
